package com.song.druid.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class RandomUtil {
    //时间戳末尾后三位
    private static volatile int GUID = 100;

    private static class TimeStampRandom {
        public static String getGUID(){
            RandomUtil.GUID += 1;
            //当前时间戳
            String now = System.currentTimeMillis() + "";
            //当前年份
            String year = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"));
            //后三位随机数
            if (RandomUtil.GUID > 999){
                RandomUtil.GUID = 100;
            }
            return year + now.substring(2) + RandomUtil.GUID;
        }
    }

    /**
     * 年份+时间戳+随机数生成18位随机ID
     * @return
     */
    public static Long timestampRandom(){
        return Long.parseLong(TimeStampRandom.getGUID());
    }
}
